package com.laoye.mapper;

import com.laoye.bean.Address;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AddressMapper {
    @Select("select id from address where user_id=#{user_id} and is_default=1")
    Integer getId(Integer user_id);

    @Insert("insert into address(user_id,receiver,phone,address,is_default) values" +
            "(#{address.user_id},#{address.receiver},#{address.phone},#{address.address},#{address.is_default})")
    void addAddress(@Param("address") Address address);

    @Select("select * from address where user_id=#{user_id} ")
    List<Address> addressList(@Param("user_id") Integer user_id);

    @Select("select * from address where user_id= #{user_id} and is_default=1")
    Address getAddress(@Param("user_id") Integer user_id);

    @Update("update address set receiver=#{address.receiver},phone=#{address.phone}," +
            "address=#{address.address},is_default=#{address.is_default} where id=#{address.id}")
    void editAddress(@Param("address") Address address);


    @Update("update address set is_default=0 where user_id= #{user_id}")
    void cancelDefault(@Param("user_id") Integer user_id);

    @Delete("delete from address where id= #{id}")
    void deleteAddress(@Param("id") Integer id);

    @Select("select * from address where id= #{addr_id}")
    Address getAddressById(@Param("addr_id") Integer addr_id);
}
